<?php
ini_set('date.timezone','Asia/Shanghai');//调整时区
//读完处理后放入数据库中

$dir = "F:\\temp";//起始路径
$hdid="000";//各个机器人的文件夹
function read_all_dir ( $dir,$hdid )//遍历文件
{
    /*数据库连接*/
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "data";

// 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }

    $result = array();
    /*读文件*/
    $handle = opendir($dir);
    if ($handle) {
        while (($file = readdir($handle)) !== false) {
            if ($file != '.' && $file != '..') {
                $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
                if (is_dir($cur_path)) {//有子目录的情况
                    $num = strrpos($cur_path,"\\");
                    $hdid = substr( $cur_path,$num+1);//读到子目录的文件夹名字
                    //echo $num." ".$hdid."<br/>";
                    $result['dir'][$cur_path] = read_all_dir($cur_path,$hdid);//读子目录下的文件
                } else {
                 //   echo 'data.'. date("Y-m-d").'_'.date("H",strtotime('-1 hour'));
               //     echo strpos($cur_path,'data.'. date("Y-m-d").'_'.date("H",strtotime('-1 hour')))."<br/>";
                    if (strpos($cur_path,'data.2017-04-10')) {//只读data.2017-04-06_11这种格式的文件
                        $result['file'][] = $cur_path;//得到要的文件的路径
                        //echo $cur_path;
                        file_put_contents("D:\phpStudy\WWW\wStatistics2\log.log",$cur_path."   ".date("Y-m-d H:i:s")."\n",FILE_APPEND );
                        $fh = fopen($cur_path, "r");//读文件
                        //处理文件数据，放入数据库中
                        $come = "加入";//判断content的预先判断值
                        $go = "通讯录";
                        if ($fh) {
                            while (!feof($fh)) {//逐条读数据
                                $line_data = fgets($fh);

                                $time = substr($line_data, 0, 19);//得到时间
                                $data = substr($line_data, 24);//去除部分，得到json
                                $obj = json_decode($data,True);
                                //print_r($obj);exit;
                                /*数据库中的数据：
                                msg的类型，判断消息类型，是否是图片
                                user_name@@和from_name判断是否是群消息（除群主外的）
                                user_id群名字，用来显示
                                content来判断是否是加群的，还有粉丝数*/
                                $mtype =$obj['msg']['MsgType'];//msg的type类型
                                $user_id = $obj['user']['id'];//用户的id
                                $user_name = $obj['user']['name'];
                                $from = $obj['msg']['FromUserName'];
                                $content = $obj['msg']['Content'];
                                $content = $conn->real_escape_string($content);//去除转义字符的情况
                              //  echo $content."<br/>";
                                //echo $time." ".$mtype." ".$user_id." ".$user_name." ".$from." ".$content." ";
                                $from = substr($from, 0, 2);//判断是否是群消息
                                if ($from === "@@") {

                                    //群消息，提交到数据库中
                                    if ($mtype == 1 || $mtype == 3)//是群消息，且不是初始化51，而是群内人发文本1，发图片3
                                    {

                                    $sql = "INSERT INTO msg (hdid,date,mtype,content,user_id,user_name) VALUES ('$hdid','$time','$mtype','$content','$user_id','$user_name')";
                                    if ($sql != NUll) {
                                        if ($conn->query($sql) === TRUE) {
                                            echo "新记录插入成功a<br/>";
                                        } else {
                                            echo "Error: " . $conn->error . "Juxin" . $sql . "<br>";
                                        }
                                    } else {
                                        echo "SQL为空a<br/>";
                                    }
                                } else if ( strpos($content, $come) &&$mtype == 10000) {//是群消息，且是系统消息come是加入群内的
                                     $sql = "INSERT INTO msg (hdid,date,mtype,content,user_id,user_name) VALUES ('$hdid','$time','$mtype','$content','$user_id','$user_name')";
                                     if ($sql != NUll) {
                                         if ($conn->query($sql) === TRUE) {
                                             echo "新记录插入成功b<br/>";
                                         } else {
                                             echo "Error: " . $conn->error . "Juxin" . $sql . "<br>";
                                         }
                                     } else {
                                         echo "SQL为空b<br/>";
                                     }
                                 }

                                } else if ($from !== "@@" && $mtype == 10000) {//不是群粉丝，系统消息，加个人好友的，go是添加到通讯录的
                                    if (strpos($content, $go)) {
                                        $sql= "INSERT INTO p_msg (hdid,date,mtype,content) VALUES ('$hdid','$time','$mtype','$content')";
                                        if($sql!=NUll) {
                                            if ($conn->query($sql) === TRUE) {
                                                echo "新记录插入成功c<br/>";
                                            } else {
                                                echo "Error: " . $conn->error . "Juxin" . $sql . "<br>";
                                            }
                                        }else{
                                            echo "SQL为空c<br/>";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        closedir($handle);//关闭路径
    }
    $conn->close();//关闭数据库
}
read_all_dir ( $dir ,$hdid)


?>